package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.secuity.MD5;
import com.ly.mpayment.core.util.CookieRedis;
import com.ly.mpayment.core.util.DateUtils;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.UserUtil;
import com.ly.mpayment.persist.entity.DownShopInfo;
import com.ly.mpayment.persist.entity.User;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;

@Component
@Path("system/downshopinfo/")
public class DownShopInfoRest extends AbsRest {

	@Autowired
	IUserService userService;

	@Autowired
	IDownShopInfoService shopInfoService;

	@Path("findDownShopPage")
	@POST
	public String findDownShopPage(String data,@Context HttpServletRequest request){

		String result = this.validationParameter(data, request, userService, "start","pageSize","useruuid","shopName");// this.validationParameter(data, request, userService, );
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isEmpty(json.getString("userstatus"))){
				parameters.put(":userstatus", json.getInt("userstatus"));
			}
			if(!StringUtils.isEmpty(json.getString("phonenum"))){
				parameters.put(":phonenum", json.getString("phonenum"));
			}

			User user = userService.findByUserid(json.getString("useruuid"));
			if(user != null && UserUtil.checkRoleAll(user)){

				if(UserUtil.checkRoleDown(user)){
					if(!StringUtils.isEmpty(json.getString("useruuid"))){
						parameters.put(":useruuid", json.getString("useruuid"));
					}
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", json.getString("shopName"));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findPageDownShopInfo(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime","downkey"}));
				}else if(UserUtil.checkRoleSupper(user)){
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", "%".concat(json.getString("shopName").concat("%")));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findPageDownShopInfo(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime",""}));
				}else if(UserUtil.checkRoleAdmin(user)){
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", "%".concat(json.getString("shopName").concat("%")));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findPageDownShopInfo(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime",""}));
				}


			}
			return ResultMessage.getFailt(ResultMessage.systemRoleError);
		}
		return result;
	}

	@Path("findBroadShopPage")
	@POST
	public String findBroadShopPage(String data,@Context HttpServletRequest request){

		String result = this.validationParameter(data, request, userService, "start","pageSize","useruuid","shopName","","","","","");// this.validationParameter(data, request, userService, );
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();

			User user = userService.findByUserid(json.getString("useruuid"));
			if(user != null && UserUtil.checkRoleAll(user)){

				if(UserUtil.checkBroadband(user)){
					if(!StringUtils.isEmpty(json.getString("useruuid"))){
						parameters.put(":useruuid", json.getString("useruuid"));
					}
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", json.getString("shopName"));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findBroadShopPage(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime","downkey"}));
				}else if(UserUtil.checkRoleSupper(user)){
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", "%".concat(json.getString("shopName").concat("%")));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findBroadShopPage(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime",""}));
				}else if(UserUtil.checkRoleAdmin(user)){
					if(!StringUtils.isEmpty(json.getString("shopName"))){
						parameters.put(":username", "%".concat(json.getString("shopName").concat("%")));
					}
					return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findBroadShopPage(parameters, json.getInt("start"),  json.getInt("pageSize")),
							new String[]{"provincialInformation","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime","downkey"}));
				}


			}
			return ResultMessage.getFailt(ResultMessage.systemRoleError);
		}
		return result;
	}

	@Path("findOneDownShop")
	@POST
	public String findOneDownShop(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
				return ResultMessage.getSucceed( JsonUtils.excludes(shopInfoService.findShopInfoByUuid(json.getString("useruuid")),
						new String[]{"provincecode","errorCodesForUseruuid","modifytime","token","","password","lastlogintime","provincestatus"}));
		}
		return result;
	}

	@Path("findOneBroadShop")
	@POST
	public String findOneBroadShop(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
				return ResultMessage.getSucceed( JsonUtils.excludes(shopInfoService.findOneBroadShop(json.getString("useruuid")),
						new String[]{"provincecode","errorCodesForUseruuid","modifytime","token","wechatid","password","lastlogintime","provincestatus"}));
		}
		return result;
	}

	@Path("updateDownShopKey")
	@POST
	public String updateDownShopKey(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			DownShopInfo shop = shopInfoService.findShopInfoByUuid(json.getString("useruuid"));
			shop.setDownkey(MD5.md5(shop.getUser().getPhonenum().concat(DateUtils.getNowDateToString())));
			if(this.shopInfoService.updateDownShopInfoDownKey(shop)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@Path("noticeDownSwitch")
	@POST
	public String noticeDownSwitch(String data,@Context HttpServletRequest request){
		String result = this.validationParameterGetRole( request, userService);
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			if(this.shopInfoService.noticeDownSwitch(json)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

	@Path("findByDownProvince")
	@POST
	public String findByDownProvince(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "provinceuuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.shopInfoService.findShopList(json.getString("provinceuuid")),
					new String[]{"phonenum","password","userstatus","usertype","token","lastlogintime","createtime","wechatid","remark",
							"provincialInformation","errorCodesForUseruuid","downshopip","downkey","downshopnum","createtime","status",
							"downshopbalance","merchantcredit","modifytime",""}));
		}
		return result;
	}

	@Path("findAllDownShopInfo")
	@POST
	public String findAllDownShopInfo(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService,"useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			User user = this.userService.findByUserid(json.getString("useruuid"));
			Map<String, Object> parameters = MapUtil.getMap();
			if(user != null){
				//检查是
				if(UserUtil.checkRoleDown(user)){
					parameters.put(":useruuid", json.getString("useruuid"));
				}
				return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.shopInfoService.findShopList(parameters),
						new String[]{"phonenum","password","userstatus","usertype","token","lastlogintime","createtime","wechatid","remark",
								"provincialInformation","errorCodesForUseruuid","downshopip","downkey","downshopnum","createtime","status",
								"","merchantcredit","modifytime",""}));
			}

		}
		return result;
	}

	@Path("findDownShopInfoKey")
	@POST
	public String findDownShopInfoKey(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "useruuid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			DownShopInfo info = this.shopInfoService.findShopInfoByUuid(json.getString("useruuid"));
			if(info != null){
				return ResultMessage.getSucceed( info.getDownkey());
			}
			return ResultMessage.getFailt("");

		}
		return result;
	}

	@Path("findDownBill")
	@POST
	public String findDownBill(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "start","pageSize","useruuid","starttime","endtime");// this.validationParameter(data, request, userService, );
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isEmpty(json.getString("starttime"))){
				parameters.put("starttime", json.getString("starttime"));
			}
			if(!StringUtils.isEmpty(json.getString("endtime"))){
				parameters.put("endtime", json.getString("endtime"));
			}

			if(!StringUtils.isEmpty(json.getString("useruuid"))){
				parameters.put("useruuid", json.getString("useruuid"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.shopInfoService.findDownBill(parameters, json.getInt("start"),  json.getInt("pageSize")),
					new String[]{""}));
		}
		return result;
	}

	@Path("getConfig")
	@POST
	public String getConfig(){
		JSONObject json = new JSONObject();
		json.put("testorderprefix",CookieRedis.get("testorderprefix"));
		json.put("testcallbackurl",CookieRedis.get("testcallbackurl"));
		return ResultMessage.getSucceed(json.toString() );
	}

}
